package com.tansun.easycare.rule.rulegroup.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tansun.easycare.rule.rulegroup.entity.RuleEntry;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroup;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;

public class RuleGroupVO {

    private RuleManage ruleManage;
    
    private List<RuleEntryVO> generalEntrys;
    
    private List<RuleEntryVO> mutualExclusions;

    
    public RuleGroupVO(RuleManage ruleManage, List<RuleGroup> ruleGroupList, List<RuleEntry> ruleEntryList) {
		super();
		this.ruleManage = ruleManage;
		this.generalEntrys = new ArrayList<RuleEntryVO>();
		this.mutualExclusions = new ArrayList<RuleEntryVO>();
		Map<String,List<RuleGroup>> map = new HashMap<String,List<RuleGroup>>();
		for(RuleGroup ruleGroup:ruleGroupList) {
			String entryId = ruleGroup.getEntryId();
			List<RuleGroup> ruleGroups = null;
			if(map.containsKey(entryId)) {
				ruleGroups = map.get(entryId);
			}else {
				ruleGroups = new ArrayList<RuleGroup>();
				map.put(entryId, ruleGroups);
			}
			ruleGroups.add(ruleGroup);
		}
        Comparator<RuleGroup> comparator1 = new Comparator<RuleGroup>() {
			public int compare(RuleGroup r1, RuleGroup r2) {
					int p1 = r1.getPriority()==null?0:Integer.parseInt(r1.getPriority());
					int s1 = r1.getSort()==null?0:r1.getSort();
					int p2 = r2.getPriority()==null?0:Integer.parseInt(r2.getPriority());
					int s2 = r2.getSort()==null?0:r2.getSort();
					return (p1-p2)*10000-(s1-s2);
			}
		};
		for(RuleEntry ruleEntry:ruleEntryList) {
			String groupType = ruleEntry.getGroupType();
			String entryId = ruleEntry.getId();
			List<RuleGroup> rules = map.get(entryId);
			if(rules!=null &&rules.size()>1) {
        		if(RuleEntry.ACCESSMODEL_PRIORITY_2.equals(ruleEntry.getAccessMode())){//优先级准入
        			Collections.sort(rules,comparator1);
                }else {
                	Collections.sort(rules);
                }
				
			}
			RuleEntryVO ruleEntryVO = new RuleEntryVO(ruleEntry,rules);
			if(RuleEntry.GROUPTYPE_GENERAL_0.equals(groupType)) {
				this.generalEntrys.add(ruleEntryVO);
			}else if(RuleEntry.GROUPTYPE_MUTEXCLU_1.equals(groupType)) {
				this.mutualExclusions.add(ruleEntryVO);
			}
		}
        Comparator<RuleEntryVO> comparator = new Comparator<RuleEntryVO>() {
			public int compare(RuleEntryVO r1, RuleEntryVO r2) {
					RuleEntry re1 = r1.getRuleEntry();
					RuleEntry re2 = r2.getRuleEntry();
					return re1.compareTo(re2);
			}
		};
		if(this.generalEntrys.size()>1) {
			Collections.sort(this.generalEntrys,comparator);
		}
		if(this.mutualExclusions.size()>1) {
			Collections.sort(this.mutualExclusions,comparator);
		}
	}

	public RuleManage getRuleManage() {
        return ruleManage;
    }

    public void setRuleManage(RuleManage ruleManage) {
        this.ruleManage = ruleManage;
    }

	public List<RuleEntryVO> getGeneralEntrys() {
		return generalEntrys;
	}

	public void setGeneralEntrys(List<RuleEntryVO> generalEntrys) {
		this.generalEntrys = generalEntrys;
	}

	public List<RuleEntryVO> getMutualExclusions() {
		return mutualExclusions;
	}

	public void setMutualExclusions(List<RuleEntryVO> mutualExclusions) {
		this.mutualExclusions = mutualExclusions;
	}

   
}
